package autotd.gui;
import autotd.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import java.util.ArrayList;

public class InformationPanel extends JPanel implements AdvancedSimulatorListener {
	
	private JTable towerTable;
	private TowerTableModel towerModel;
	private JScrollPane towerScrollPane;
	
	private JTable monsterTable;
	private MonsterTableModel monsterModel;
	private JScrollPane monsterScrollPane;
	
	private TDSimulator sim;
	private Point towerStackPoint = null;
	private List<Monster> monsterList = new ArrayList<Monster>();
	
	public InformationPanel(){
		super();
		
		setPreferredSize(new Dimension(200, 0));
		
		towerModel = new TowerTableModel(new String[] {"Tower"}, 0);
		towerTable = new JTable(towerModel);
		towerScrollPane = new JScrollPane(towerTable);
		towerScrollPane.setPreferredSize(new Dimension(200,300));

		add(towerScrollPane);
		
		monsterModel = new MonsterTableModel(new String[] {"Monster", "Health"}, 0);
		monsterTable = new JTable(monsterModel);
		monsterScrollPane = new JScrollPane(monsterTable);
		monsterScrollPane.setPreferredSize(new Dimension(200,300));

		add(monsterScrollPane);
	
	}
	
	public void setSimulator(TDSimulator sim) {
		this.sim = sim;
		sim.addAdvancedListener(this);
	}
	
	public void setTowerStackPoint(Point p) {
		towerStackPoint = p;
		updateTowerTable();
	}
	
	public void setMonsterList(List<Monster> monsters) {
		if (monsters!=null) monsterList = monsters;
		updateMonsterTable();
	}
	
	public void updateTowerTable() {
		/*
		towerModel = new TowerTableModel(new String[] {"Tower"}, 0);
		
		List<Tower> towers = new ArrayList<Tower>();
		if (sim!=null && towerStackPoint!=null) towers = sim.getMap().getTowerStack(towerStackPoint);
		if (towers==null) towers = new ArrayList<Tower>();
		
		for(Tower t: towers) {
			Vector<String> tmp = new Vector<String>();
			tmp.add(t.Type.Name);
			towerModel.addRow(tmp);
		}
		
		towerTable.setModel(towerModel);*/
	}
	
	public void updateMonsterTable() {
			/*	
		monsterModel = new MonsterTableModel(new String[] {"Monster", "Health"}, 0);
		
		for(Monster m: monsterList) {
			Vector<String> tmp = new Vector<String>();
			tmp.add(m.Type.Name);
			tmp.add(m.health+"");
			monsterModel.addRow(tmp);
		}
		
		monsterTable.setModel(monsterModel);*/
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		updateTowerTable();
		updateMonsterTable();
	}
	
	class TowerTableModel extends DefaultTableModel {
		
		public TowerTableModel(String[] headers, int rows) {
			super(headers, rows);
		}
		
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		
	}
	
	class MonsterTableModel extends DefaultTableModel {
		
		public MonsterTableModel(String[] headers, int rows) {
			super(headers, rows);
		}
		
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		
	}

	public void monsterCreated(int mID, String type, int level, int x, int y) {
		updateMonsterTable();
	}
	public void monsterMoved(int mID, int prevX, int prevY, int currX, int currY) {
		updateMonsterTable();
	}
	public void monsterInjured(int mID, int health) {
		updateMonsterTable();
	}
	public void monsterCompletedPath(int mID) {
		updateMonsterTable();
	}
	public void towerPlaced(int tID, String type, int x, int y, int height) {
		updateTowerTable();
	}
	public void towerFired(int tID, int mID) {
		updateTowerTable();
	}
	public void playerChange(int lives, int money) {}
	public void turnChangedTo(int turnNum) {}
}
